
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;


namespace CinemaLib
{
    /// <summary>
    /// Contains Hall Properties via WCF.
    /// </summary>
    [DataContract]
    public class Hall
    {
        [DataMember]
        public int Number { get; private set; }
        [DataMember]
        public List<Row> Rows { get; private set; }
        private List<Show> ShowSchedule { get; set; }

        /// <summary>
        /// Index.
        /// </summary>
        /// <param name="i">Select Index of Row</param>
        /// <returns>Return row of an Index</returns>
        public Row this[int i]
        {
            get { return Rows[i]; }
            set { Rows[i] = value; }
        }

        public Hall(int _number)
        {
            Number = _number;
            Rows = new List<Row>();
            ShowSchedule = new List<Show>();
        }

        public Hall(int _number, List<Row> _rows)
        {
            Number = _number;
            Rows = _rows;
            ShowSchedule = new List<Show>();
        }

        public bool AddShow(Show _show)
        {
            foreach(Show s in ShowSchedule)
            {
                if (s.StartTime <= _show.StartTime && s.StartTime.Add(s.Length) >= _show.StartTime )
                {
                    return false;
                }
            }
            ShowSchedule.Add(_show);
            return true;
        }

        public int CountTotal()
        {
            int ts = 0;
            foreach (Row r in Rows)
            {
                foreach (Seat s in r.Seats)
                {
                    ts++;
                }
            }
            return ts;
        }

        public int CountAvaible()
        {
            int os = 0;
            foreach (Row r in Rows)
            {
                foreach (Seat s in r.Seats)
                {
                    if (!s.Occupied)
                        os++;
                }
            }
            return os;
        }

        public bool RemoveShow(Show _show)
        {
            if (DateTime.Now.Subtract(_show.StartTime).TotalHours <= (double)-1.0)
            {
                return false;
            }
            else
            {
                ShowSchedule.Remove(_show);
                return true;
            }
        }

        public bool RemoveShow(int _index)
        {
            if (DateTime.Now.Subtract(ShowSchedule[_index].StartTime).TotalHours <= (double)-1.0)
            {
                return false;
            }
            else
            {
                ShowSchedule.RemoveAt(_index);
                return true;
            }
        }

        public List<Show> GetSchedule()
        {
            return ShowSchedule.AsReadOnly().ToList<Show>();
        }
    }
}
